package com.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 描述:
 *
 * @author luzhongfu
 * Created by lucky on 2022-02-17 4:55 PM
 */
public class ConditionTest {

    private final Lock lock = new ReentrantLock();

    private final Condition condition = lock.newCondition();

    public static void main(String[] args) throws InterruptedException {
        ConditionTest test = new ConditionTest();

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    test.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();
        Thread.sleep(3000);
//        Thread thread = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                test.notify2();
//            }
//        });
//        thread.start();
        System.out.println();

    }

    private void await() throws InterruptedException {

        lock.lock();
        condition.await(1000, TimeUnit.MILLISECONDS);
        System.out.println("222222");
        lock.unlock();
    }

    public void notify2(){
        lock.lock();
        condition.signal();
        lock.unlock();
    }
}
